<template>
  <div class="fullscreen-container">
    <div class="content-wrapper">
      <el-card style="width: 100%;">
      <template #header>
        <div class="card-header">
          <span>课程选择</span>
        </div>
      </template>
      <div class="search-bar">
        <el-input v-model="searchQuery" placeholder="请输入课程名称" prefix-icon="Search" style="width: 400px;" />
        <el-select v-model="teacherFilter" placeholder="选择教师" style="width: 200px; margin-left: 10px;">
          <el-option label="全部教师" value=""></el-option>
          <el-option label="张老师" value="1"></el-option>
          <el-option label="李老师" value="2"></el-option>
          <el-option label="王老师" value="3"></el-option>
        </el-select>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
      <div class="course-grid">
        <el-card
          v-for="course in coursesData"
          :key="course.courseId"
          class="course-card"
          :body-style="{ padding: '16px' }"
        >
          <div class="course-header">
            <h3 class="course-name">{{ course.courseName }}</h3>
            <el-tag :type="course.credit >= 3 ? 'success' : 'info'">{{ course.credit }}学分</el-tag>
          </div>
          <div class="course-info">
            <p><strong>教师：</strong>{{ course.teacherName }}</p>
            <p><strong>课时：</strong>{{ course.hours }}课时</p>
            <p><strong>上课时间：</strong>{{ course.classTime }}</p>
            <p><strong>上课地点：</strong>{{ course.location }}</p>
            <p><strong>已选人数：</strong>{{ course.selectedCount }}/{{ course.maxCount }}</p>
          </div>
          <div class="course-desc">{{ course.description }}</div>
          <div class="course-actions">
            <el-button
              v-if="!selectedCourses.includes(course.courseId) && course.selectedCount < course.maxCount"
              type="primary"
              @click="handleSelect(course)"
            >选择课程</el-button>
            <el-button
              v-else-if="selectedCourses.includes(course.courseId)"
              type="success"
              disabled
            >已选择</el-button>
            <el-button
              v-else
              disabled
            >名额已满</el-button>
          </div>
        </el-card>
      </div>
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'

// 模拟课程数据
const coursesData = ref([
  {
    courseId: 1,
    courseName: '高等数学',
    teacherName: '张老师',
    credit: 5,
    hours: 80,
    classTime: '周一、周三 08:00-09:40',
    location: '教学楼101',
    selectedCount: 45,
    maxCount: 60,
    description: '高等数学是理工科专业的基础课程，主要内容包括微积分、微分方程等。'
  },
  {
    courseId: 2,
    courseName: '线性代数',
    teacherName: '李老师',
    credit: 4,
    hours: 64,
    classTime: '周二、周四 10:00-11:40',
    location: '教学楼202',
    selectedCount: 55,
    maxCount: 60,
    description: '线性代数是研究向量空间和线性变换的数学分支，是计算机科学的重要基础。'
  },
  {
    courseId: 3,
    courseName: '概率论',
    teacherName: '王老师',
    credit: 3,
    hours: 48,
    classTime: '周五 08:00-11:40',
    location: '实验楼301',
    selectedCount: 60,
    maxCount: 60,
    description: '概率论是研究随机现象数量规律的数学分支，广泛应用于各领域。'
  }
])

// 已选课程ID数组
const selectedCourses = ref<number[]>([])

const searchQuery = ref('')
const teacherFilter = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(coursesData.value.length)

const handleSelect = (course: any) => {
  ElMessageBox.confirm(`确定要选择课程《${course.courseName}》吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    selectedCourses.value.push(course.courseId)
    // 更新已选人数
    course.selectedCount++
    ElMessage.success('课程选择成功')
  }).catch(() => {
    // 取消选择
  })
}

const handleSearch = () => {
  // 搜索功能
  ElMessage.info(`搜索：${searchQuery.value}，教师：${teacherFilter.value}`)
}

const handleSizeChange = (size: number) => {
  pageSize.value = size
}

const handleCurrentChange = (current: number) => {
  currentPage.value = current
}
</script>

<style scoped>
.fullscreen-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-color: #f5f5f5;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  overflow: hidden;
}

.content-wrapper {
  width: 100%;
  height: 100%;
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
  align-items: center;
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.course-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.course-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.course-name {
  margin: 0;
  font-size: 18px;
}

.course-info {
  margin-bottom: 16px;
}

.course-info p {
  margin: 5px 0;
  font-size: 14px;
}

.course-desc {
  margin-bottom: 20px;
  font-size: 14px;
  color: #606266;
  flex: 1;
}

.course-actions {
  display: flex;
  justify-content: flex-end;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>